package com.heima.admin.service.impl;

import com.heima.admin.service.SensitiveService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.AdSensitiveDto;
import com.heima.model.admin.dtos.AdSensitiveListDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.wemedia.ISensitiveClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author Franky
 * @version 1.0
 * @project heima-leadnews-shizhan
 * @description
 * @date 2023/9/17 10:01:02
 */
@Service
@Transactional
@Slf4j
public class SensitiveServiceImpl implements SensitiveService {

    @Autowired
    private ISensitiveClient iSensitiveClient;

    @Override
    public ResponseResult del(Integer id) {
        //参数校验
        if (id == null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //远程调用article的feign，执行删除
        return iSensitiveClient.del(id);
    }

    @Override
    public ResponseResult list(AdSensitiveListDto sensitiveListDto) {
        //参数校验
        if (Objects.isNull(sensitiveListDto)){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        if (sensitiveListDto.getSize() >20){
            sensitiveListDto.setSize(20);
        }

        //远程调用feign，执行列表查询
        return iSensitiveClient.list(sensitiveListDto);
    }

    @Override
    public ResponseResult save(AdSensitiveDto sensitiveDto) {
        //参数校验
        if (Objects.isNull(sensitiveDto)){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //远程调用feign，执行新增
        return iSensitiveClient.save(sensitiveDto);
    }

    @Override
    public ResponseResult update(AdSensitiveDto sensitiveDto) {
        //参数校验
        if (Objects.isNull(sensitiveDto) || StringUtils.isBlank(sensitiveDto.getSensitives()) || sensitiveDto.getId() == null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //远程调用feign，执行修改
        return iSensitiveClient.update(sensitiveDto);
    }
}
